package com.example.desksginktapp.views

import android.os.Bundle
import android.widget.Toast
import androidx.activity.result.ActivityResultLauncher
import androidx.lifecycle.lifecycleScope
import com.example.desksginktapp.databinding.ActivityScanQrBinding
import com.example.desksginktapp.viewmodels.ScanQrViewModel
import com.journeyapps.barcodescanner.ScanContract
import com.journeyapps.barcodescanner.ScanOptions
import com.journeyapps.barcodescanner.camera.CameraSettings
import com.zyf.baselibrary.base.BaseViewBindingActivity
import kotlinx.coroutines.launch

class ScanQrActivity : BaseViewBindingActivity<ActivityScanQrBinding, ScanQrViewModel>() {
    private var isProcessingScan = false
    private lateinit var scanLauncher: ActivityResultLauncher<ScanOptions>
    private var lastScanTime = 0L
    private val scanInterval = 1000L // 1秒扫描间隔防止重复扫描

    override fun provideViewModel(): ScanQrViewModel {
        return ScanQrViewModel()
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        initScanLauncher()
    }

    override fun initData() {
        super.initData()
        setupScanner()
    }

    private fun initScanLauncher() {
        scanLauncher = registerForActivityResult(ScanContract()) { result ->
            if (result.contents != null && System.currentTimeMillis() - lastScanTime > scanInterval) {
                lastScanTime = System.currentTimeMillis()
                handleScanResult(result.contents)
            }
        }
    }


    private fun setupScanner() {
        binding.zxingBarcodeScanner.barcodeView.apply {
            cameraSettings = CameraSettings().apply {
                isAutoFocusEnabled = true
                focusMode = CameraSettings.FocusMode.CONTINUOUS
            }

            decodeContinuous { result ->
                result?.text?.let { text ->
                    // 检查是否正在处理以及防抖
                    if (!isProcessingScan && System.currentTimeMillis() - lastScanTime > scanInterval) {
                        isProcessingScan = true
                        lastScanTime = System.currentTimeMillis()
                        handleScanResult(text)
                    }
                }
            }
        }
    }


    private fun handleScanResult(resultText: String) {
        lifecycleScope.launch {
            try {
                Toast.makeText(this@ScanQrActivity, "扫描结果: $resultText", Toast.LENGTH_SHORT).show()
                // 处理扫描结果
                // viewModel.processScanResult(resultText)
            } finally {
                // 处理完成后重置标记
                isProcessingScan = false
            }
        }
    }
    override fun onResume() {
        super.onResume()
        binding.zxingBarcodeScanner.resume()

    }

    override fun onPause() {
        super.onPause()
        binding.zxingBarcodeScanner.pause()
    }

    override fun onDestroy() {
        super.onDestroy()
        binding.zxingBarcodeScanner.barcodeView.destroyDrawingCache()
    }

    companion object {
        private const val TAG = "ScanQrActivity"
    }
}